package com.moli.iov.real.service.impl;


import com.moli.iov.protocol.model.realtime.RealTimeInfo;
import com.moli.iov.protocol.model.realtime.RealTimeInfoReq;
import com.moli.iov.real.model.req.RealTimeReqDto;
import com.moli.iov.real.repositories.RealTimeDao;
import com.moli.iov.real.service.RealTimeService;
import com.moli.iov.util.StringUtil;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service("realTimeService")
@Slf4j
public class RealTimeServiceImpl implements RealTimeService {

    @Resource
    RealTimeDao realTimeDao;

    @Override
    public Date getLastReportTime(String vin){

        long start = System.currentTimeMillis();
        RealTimeInfoReq real = this.getRealTimeInfoReq(vin);
        long end = System.currentTimeMillis();
        log.info("执行时长:{}",end-start);
        if(real!=null && real.getCollectDate()!=null){
            return real.getCollectDate().getDate();
        }
        return null;
    }

    @Override
    public Map<String, Date> getLastReportTimeMap(List<String> vinList) {
        Map<String,Date> map = new HashMap();
        Query query = assemblyConditions(vinList);
        List<RealTimeInfo> list = realTimeDao.find(query);
        if(list==null || list.size()<1){
            return null;
        }
        for (RealTimeInfo realTimeInfo : list) {
            if(realTimeInfo!=null
                && !StringUtils.isEmpty(realTimeInfo.getVin())
                && realTimeInfo.getRealTimeInfoReq()!=null
                && realTimeInfo.getRealTimeInfoReq().getCollectDate()!=null){
                map.put(realTimeInfo.getVin(),realTimeInfo.getRealTimeInfoReq().getCollectDate().getDate());
            }
        }
        return map;
    }

    @Override
    public RealTimeInfoReq getRealTimeInfoReq(String vin){
        long start = System.currentTimeMillis();
        if(StringUtils.isEmpty(vin)){
            return null;
        }

        RealTimeInfo res = realTimeDao.findOne(assemblyConditions(new RealTimeReqDto().putVin(vin)));

        long end = System.currentTimeMillis();
        log.info("执行时长:{}",end-start);
        if(res!=null ){
            return res.getRealTimeInfoReq();
        }
        return null;
    }

    @Override
    public RealTimeInfo getRealTimeInfo(String vin){
        long start = System.currentTimeMillis();
        if(StringUtils.isEmpty(vin)){
            return null;
        }

        RealTimeInfo res = realTimeDao.findOne(assemblyConditions(new RealTimeReqDto().putVin(vin)));

        long end = System.currentTimeMillis();
        log.info("执行时长:{}",end-start);

        return res;
    }

    @Override
    public List<RealTimeInfo> listRealTimeInfo(List<String> vins){
        long start = System.currentTimeMillis();
        List<RealTimeInfo> res = realTimeDao.find(assemblyConditions(new RealTimeReqDto().putVins(vins)));
        long end = System.currentTimeMillis();
        log.info("执行时长:{}",end-start);
        return res;
    }

    private Query assemblyConditions(RealTimeReqDto realTimeReqDto) {
        Criteria criteria = new Criteria();

        if(realTimeReqDto!=null){
            if (StringUtil.isNotBlank(realTimeReqDto.getVin())) {
                criteria.and("vin").is(realTimeReqDto.getVin());
            }

            if (realTimeReqDto.getVins()!=null && realTimeReqDto.getVins().size()>0) {
                criteria.and("vin").in(realTimeReqDto.getVins());
            }
        }

        Query query = new Query(criteria);
        return query;
    }

    private Query assemblyConditions(List<String> vinList) {
        Criteria criteria = new Criteria();

        if(vinList!=null && vinList.size()>0){
            criteria.and("vin").in(vinList);
        }

        Query query = new Query(criteria);
        return query;
    }
}
